package com.ygxsk.carhome.ui.home;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Window;
import android.view.WindowManager;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import com.umeng.message.UmengNotifyClick;
import com.umeng.message.entity.UMessage;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.constant.ConstantSign;
import com.ygxsk.carhome.constant.ExtraKey;
import com.ygxsk.carhome.constant.ExtraValue;
import com.ygxsk.carhome.ui.SplashActivity;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.functions.Consumer;

/*
 * @Author:itchenqi175@163.com
 * @Time:2023-11-17  10:47
 * @Description:消息推送的厂商推送集成点击逻辑
 */
public class MfrMessageActivity extends Activity {

    private final UmengNotifyClick mNotificationClick = new UmengNotifyClick() {
        @Override
        public void onMessage(UMessage msg) {

            final String body = msg.getRaw().toString();

            LogUtils.d("itchen--------onMessage---onMessage------onMessage---onMessage------onMessage---onMessage------onMessage---onMessage------onMessage---onMessage");

            LogUtils.d(ConstantSign.LOGCAT_NOMAL, "body: " + body);

            if (!TextUtils.isEmpty(body)) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
//                        ((TextView) findViewById(R.id.tv)).setText(body);

                        //放在主线程执行.
                        Observable.just(0)//
                                .subscribeOn(AndroidSchedulers.mainThread())//
                                .observeOn(AndroidSchedulers.mainThread())//
                                .subscribe(new Consumer<Integer>() {
                                    @Override
                                    public void accept(Integer integer) throws Exception {

                                        //这里有两种情况，如果应用活着的，那么直接打开消息页面
                                        if (AppUtils.isAppRunning("com.ygxsk.carhome")) {

//                            LogUtils.d("itchen--HuaWeiRegister---AppUtils.isAppRunning--正在运行着的，是活的");

                                            if (AppUtils.isAppForeground()) {//如果是前台运行那么，直接跳转页面。

//                                LogUtils.d("itchen---HuaWeiRegister--HomeMainActivity-在前台运行着的--");

                                                //什么前置条件操作？
                                                methodJumpToHome();

                                            } else {

                                                boolean isActivityExistsInStack = ActivityUtils.isActivityExistsInStack(HomeMainFragmentActivity.class);
//                                LogUtils.d("itchen---HuaWeiRegister--HomeMainActivity-应用是否在堆栈之中-->" + isActivityExistsInStack);

                                                if (isActivityExistsInStack) {
                                                    //如果栈顶的正是HomeMainActivity,那么直接打开消息页面
                                                    methodJumpToHome();

                                                } else {

//                                    LogUtils.d("itchen---HuaWeiRegister--HomeMainActivity-确认不在堆栈之中-->那么直接启动 SplashActivity");

                                                    //如果是后台运行，那么重启app即可
                                                    //启动app-->进而启动消息内容
                                                    Intent intentTosplash = new Intent();
                                                    intentTosplash.setClass(MfrMessageActivity.this, SplashActivity.class);
                                                    intentTosplash.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                                    MfrMessageActivity.this.startActivity(intentTosplash);

                                                }

                                            }

                                        } else {

//                            LogUtils.d("itchen--HuaWeiRegister--应用没有打开，那么直接打开APP----> AppUtils.launchApp(com.dgj.propertysmart)");

                                            //APP是没有启动的，那么就直接启动APP
                                            AppUtils.launchApp("com.dgj.propertysmart");

                                        }

                                    }
                                });


                        //=============================================================================================================================================

                    }
                });

            }
        }
    };

    /**
     * 直接跳转到首页的
     */
    private void methodJumpToHome() {

        //携带参数内容
        Intent intentToMessage = new Intent();
        //跳转到【首页】并且跳转到【我的消息】。
        intentToMessage.setClass(MfrMessageActivity.this, HomeMainFragmentActivity.class);
        intentToMessage.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//必须携带吗？
        //携带一个跳转到消息选项卡的标识
        intentToMessage.putExtra(ExtraKey.EXTRA_LOGINKEY, ExtraValue.EXTRA_PUSHTOBULLETIN);
        startActivity(intentToMessage);

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏

        setContentView(R.layout.activity_mfr_message);

        if (mNotificationClick != null) {
            mNotificationClick.onCreate(this, getIntent());
        }

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        if (mNotificationClick != null) {
            mNotificationClick.onNewIntent(intent);
        }

    }
}